package com.dut.bbs.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dut.bbs.R;
import com.dut.bbs.adapter.Face;
import com.dut.bbs.adapter.FaceAdapter;
import com.dut.bbs.entity.MailEntity;
import com.dut.bbs.entity.PostEntity;
import com.dut.bbs.util.Dg;
import com.dut.bbs.util.Settings;

public class PostActivity extends BaseActivity {
	private EditText et_title;
	private EditText et_content,et_mailID;
	private TextView tv_revID;
	private Button bt_ok,bt_cancel;
	private ImageButton bt_exp;
	private Context context;
	private Dialog dg_face;
	private GridView faceGrid;
	private CheckBox ck_sign;
	private int flag;
	private Handler handler = new Handler(){
		@Override  
        public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				Dg.showDg(context, "发送成功！");
				break;
			case 0:
				Dg.showDg(context, "发送失败！");
				break;
			}
			getProgressDialog().dismiss();
		}
	};
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reply);
		context = PostActivity.this;
		Intent intent=getIntent();
		flag = intent.getIntExtra("flag", 1);
		final String replyURL = intent.getStringExtra("url");
		String replytitle = intent.getStringExtra("title");
		TextView tv_title = (TextView) findViewById(R.id.activitytitle);
		tv_revID = (TextView) findViewById(R.id.mailidtv);
		et_title = (EditText) findViewById(R.id.replaytitle);
		et_mailID = (EditText) findViewById(R.id.et_mailID);
		et_content = (EditText) findViewById(R.id.replycontent);
		ck_sign = (CheckBox) findViewById(R.id.ck_sign);
		bt_exp = (ImageButton) findViewById(R.id.bt_express);
		tv_title.setText("发帖");
		if(2==flag){
			et_title.setText("Re"+replytitle);
			et_title.setEnabled(false);
			if(intent.getStringExtra("id")!=null)
				et_content.setText("【 在 "+intent.getStringExtra("id")+" 的大作中提到: 】");
			tv_title.setText("回复主题");
		}else if(3==flag){
			tv_title.setText("发信私信");
			ck_sign.setText("备份发信箱");
			tv_revID.setVisibility(View.VISIBLE);
			et_mailID.setVisibility(View.VISIBLE);
			et_mailID.setText(intent.getStringExtra("id"));
			et_mailID.setEnabled(false);
		}else if(4==flag){
			tv_title.setText("发送私信");
			ck_sign.setText("备份发信箱");
			tv_revID.setVisibility(View.VISIBLE);
			et_mailID.setVisibility(View.VISIBLE);
		}else if(5==flag){
			tv_title.setText("发信私信");
			ck_sign.setText("备份发信箱");
			tv_revID.setVisibility(View.VISIBLE);
			et_mailID.setVisibility(View.VISIBLE);
			et_mailID.setText(intent.getStringExtra("id"));
			et_mailID.setEnabled(false);
			et_title.setText(intent.getStringExtra("title"));
			et_title.setEnabled(false);
		}
		bt_ok = (Button) findViewById(R.id.replysubmit);
		bt_cancel = (Button) findViewById(R.id.replycancel);
		bt_cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				PostActivity.this.finish();
			}
		});
		bt_exp.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Tip tip = new Tip(context);
				tip.showdg();
			}
		});
		bt_ok.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				getProgressDialog().setMessage("发送信息中...");
				getProgressDialog().show();
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						String title = et_title.getText().toString();
						String content = et_content.getText().toString();
						if (flag == 1 || flag == 2) {
							if(Settings.showSign&&ck_sign.isChecked()){
								String str = Settings.sign;
								content = content.replaceAll("## ", Settings.quickReply);
								content+="\n[color=#"+Settings.signColor+"]"+str+"[/color]";
								if(Settings.showPhone)
									content+=" [color=#"+Settings.signColor+"]by [i]"+android.os.Build.MODEL+"[/i][/color]";
							}
							if (PostEntity.post(content, title, replyURL))
								handler.sendEmptyMessage(1);
							else
								handler.sendEmptyMessage(0);
						}else{
							String id = et_mailID.getText().toString();
							content+="\n[color=#"+Settings.signColor+"]发送自碧海青天Android客户端[/color]";
							if(MailEntity.mail(id, title, content, ck_sign.isChecked()))
								handler.sendEmptyMessage(1);
							else
								handler.sendEmptyMessage(0);
						}
					}
				}).run();
//				String title = et_title.getText().toString();
//				String content = et_content.getText().toString();
//				if(Settings.showSign&&ck_sign.isChecked()){
//					String str = Settings.sign;
//					content+="\n[color=#"+Settings.signColor+"]"+str+"[/color]";
//					if(Settings.showPhone)
//						content+=" [color=#"+Settings.signColor+"]by [i]"+android.os.Build.MODEL+"[/i][/color]";
//				}
//				if(PostEntity.post(content, title, replyURL)){
//					Dg.showDg(context, "发表成功！");
//				}else
//					Dg.showDg(context, "发表失败！");
			}
		});
	}
	class Tip {        // 弹出表情层的类  
		public Tip(Context context) {
			dg_face = new Dialog(context, R.style.facedialog); // 定义一个Dialog
			dg_face.setCanceledOnTouchOutside(true);													// ，Style样式：设置背景透明，无title，无边框
			Window window = dg_face.getWindow();
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = -30;
			wl.y = 20;
			window.setAttributes(wl);
			window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			dg_face.setContentView(R.layout.facegridview);
			dg_face.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0);
			faceGrid = (GridView) dg_face.findViewById(R.id.updater_faceGrid);
			if (faceGrid.getAdapter() == null) {
				faceGrid.setAdapter(new FaceAdapter(getApplicationContext(),
						Face.faceNames));
			}
			faceGrid.setOnItemClickListener(new OnItemClickListener() { // Gridview选中，选择一个表情

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					Toast.makeText(getBaseContext(), Face.faceNames[position],
							Toast.LENGTH_SHORT).show();
					// dialog.dismiss();
					et_content.append("[" + Face.faceNames[position] + "]");
				}
			});
		}

		void showdg() {
			dg_face.show();
		}

	}
}
